package com.gitee.wsl.jvm.ext.base

import java.util.Collections
import java.util.TreeMap
import java.util.LinkedHashMap

/**
 * Updates the receiver [TreeMap] with the contents of another [Map].
 */
fun <K, V> TreeMap<K, MutableList<V>>.updateAll(other: Map<K, List<V>>) {
    val self = this
    other.forEach { (key, value) ->
        self[key] = self[key]?.apply { addAll(value) } ?: value.toMutableList()
    }
}


fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> = when (size) {
    0 -> emptyMap()
    1 -> entries.first().let { (key, value) -> Collections.singletonMap(key, value) }
    else -> Collections.unmodifiableMap(LinkedHashMap(this))
}